import 'package:async/async.dart';
import 'package:boss/common/common.dart';
import 'package:boss/http/api/api_helper.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/models/store_menu.dart';
import 'package:flutter/material.dart';

class PickerCommodityProvider extends ChangeNotifier {
  //菜品分类及菜品数据
  List<StoreMenu> storeMenus = [];

  //当前选中的菜品分类下表
  int storeMenuSelectIndex = 0;

  //当前所有的菜品列表
  List<Commodity> get commoditys {
    List<Commodity> list = [];

    this.storeMenus.forEach((menu) {
      menu.commodityList.forEach((commodity) {
        list.add(commodity);
      });
    });

    return list;
  }

  //当前选中的菜品菜品列表
  List<Commodity> get currentCommoditys {
    if (storeMenus.length == 0) {
      return [];
    }
    return storeMenus[storeMenuSelectIndex].commodityList ?? [];
  }

  //套餐分类
  List<StoreMenu> get packageStoreMenus => storeMenus.where((item) => item.isPackages == 1).toList();

  //不包含套餐分类
  List<StoreMenu> get normalStoreMenus => storeMenus.where((item) => item.isPackages != 1).toList();

  int get allCommodityCount => commoditys.length;

  //当前选中的分类
  StoreMenu get currentMenuCategory {
    if (storeMenus.length == 0) return null;

    return storeMenus[storeMenuSelectIndex];
  }

  bool get isSelectedAllCategoryCommodity {
    return currentCommoditys.every((element) => element.isSelected);
  }

  //操作类型
  int type = 0;

  bool isShowSku;

  bool isShowPackage;

  //选中的菜品
  List<Commodity> get selectedCommoditys => commoditys.where((commodity) => commodity.isSelected).toList();

  PickerCommodityProvider(
    this.type,
    this.isShowSku,
    this.isShowPackage,
  );

  //设置菜品分类
  setStoreMenus(List<StoreMenu> storeMenus) {
    this.storeMenus = storeMenus;

    notifyListeners();
  }

  //设置菜品选中下标
  setStoreMenuSelectIndex(int index) {
    storeMenuSelectIndex = index;
    notifyListeners();
  }

  //获取数据
  Future<Result<List<StoreMenu>>> initData({
    List<Commodity> selectedCommoditys,
    bool isShowPackage,
    List<int> filterType,
    bool isShowSku,
  }) async {
    try {
      var res = await queryMenuCategory();
      if (!res.successful()) {
        return Result.error(Constant.httpErrText);
      }

      if (!isShowPackage) {
        res.data.removeWhere((menu) => menu.isPackages == 1);
      }

      res.data.forEach((menu) {
        List<Commodity> commoditys = [];

        menu.commodityList.forEach((commodity) {
          if (commodity.commoditySkuList.length > 0 && isShowSku) {
            commodity.commoditySkuList.forEach((sku) {
              var temp = Commodity.fromJson(commodity.toJson());
              temp.sku = sku;

              var finder = selectedCommoditys?.firstWhere((selected) => selected.id == temp.id && selected.sku?.skuId == temp.sku?.skuId, orElse: () => null);

              temp.isSelected = finder != null;

              if (type == 1) temp.count = finder?.count ?? 0;

              commoditys.add(temp);
            });
          } else {
            var temp = Commodity.fromJson(commodity.toJson());

            var finder = selectedCommoditys?.firstWhere((selected) => selected.id == temp.id, orElse: () => null);
            temp.isSelected = finder != null;

            if (type == 1) temp.count = finder?.count ?? 0;

            commoditys.add(temp);
          }
        });
        menu.commodityList = commoditys;
      });

      if (filterType != null && filterType.length > 0) {
        res.data.forEach((menu) {
          menu.commodityList.removeWhere((commodity) => filterType.contains(commodity.commodityType));
        });
      }

      setStoreMenus(res.data);
      return Result.value(res.data);
    } catch (e) {
      print(e);
      return Result.error(e.toString());
    }
  }

  //设置菜品数量
  setCommodityCount(Commodity commodity, int count) {
    commodity.count = count;
    commodity.isSelected = count > 0;
    notifyListeners();
  }

  toggleCommoditySelected(Commodity commodity) {
    commodity.isSelected = !commodity.isSelected;
    notifyListeners();
  }

  void selectedAllCategoryCommodity() {
    final selected = !isSelectedAllCategoryCommodity;
    currentCommoditys.forEach((element) {
      element.isSelected = selected;
    });
    notifyListeners();
  }
}
